package com.fom2008;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

import com.fom2008.db.FomTag;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class FomTagView extends MapActivity {

	private MapView					mapView					= null;

	private Cursor					point;

	private static final int		INDEX_POINT_CONTENT_URI	= 0;
	private static final int		INDEX_POINT_LATITUDE	= 1;
	private static final int		INDEX_POINT_LONGITUDE	= 2;
	private static final String[]	PROJECTION				= new String[] { FomTag.Tags.TAGGED_CONTENT, // 0
			FomTag.Tags.LATITUDE, // 1
			FomTag.Tags.LONGITUDE							// 2
															};

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.map_view);

		mapView = (MapView) findViewById(R.id.map);

		// Add zoom controls
		LinearLayout zoomLayout = (LinearLayout) findViewById(R.id.map_zoom);
		zoomLayout.addView(mapView.getZoomControls(), new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));
	}

	@Override
	protected void onStart() {
		super.onStart();

		// This activity started with an intent. The intent's data contains the
		// URI we are working on. From that we can get the longitude, latitude
		Intent intent = getIntent();
		if (intent == null) {
			// Something went wrong...
		} else {

			// Query the database. The managedQuery lets the activity handle the
			// cursor (mainly closing/opening) compare to ContentResolver.query
			// method
			point = managedQuery(intent.getData(), PROJECTION, null, null, null);

			Drawable marker = getResources().getDrawable(R.drawable.marker);
			marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight());

			mapView.getOverlays().add(new TagOverlay(marker, point));

			// Move to the first position.
			if (point.moveToFirst()) {
				// From the point, we get the lat/long. Be careful with the
				// values as GeoPoint takes 1E6 values while the GPS will return
				// long value
				GeoPoint p = new GeoPoint((int) (point.getDouble(INDEX_POINT_LATITUDE) * 1E6), (int) (point
						.getDouble(INDEX_POINT_LONGITUDE) * 1E6));

				mapView.getController().setCenter(p);
				mapView.getController().setZoom(12);
			}
		}
	}

	public MapView getMapView() {
		return mapView;
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	private class TagOverlay extends ItemizedOverlay<OverlayItem> {

		private Cursor		cur		= null;

		private Drawable	marker	= null;

		private TagOverlay(Drawable drawable, Cursor cur) {
			super(drawable);
			this.cur = cur;
			this.marker = drawable;
			this.populate();
		}

		@Override
		protected OverlayItem createItem(int i) {
			cur.moveToPosition(i);
			int lat = (int) (cur.getDouble(INDEX_POINT_LATITUDE) * 1E6);
			int lon = (int) (cur.getDouble(INDEX_POINT_LONGITUDE) * 1E6);
			GeoPoint p = new GeoPoint(lat, lon);
			return new OverlayItem(p, "test", "testsdd");
		}

		@Override
		public int size() {
			return cur.getCount();
		}

		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			super.draw(canvas, mapView, shadow);
			boundCenterBottom(marker);
		}

		@Override
		protected boolean onTap(int index) {
			cur.moveToPosition(index);

			// We launch the view activity for the tagged content
			Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(cur.getString(INDEX_POINT_CONTENT_URI)));
			FomTagView.this.startActivity(intent);
			return super.onTap(index);
		}
	}

}
